﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;

namespace kelvinDigital.USiteBuilderAdmin
{

    public partial class usercontrols_NestedRepeater : System.Web.UI.UserControl
    {
        public List<ContentComparison> Data { get; set; }
        public string ParentAlias { get; set; }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            rptList.DataSource = Data.Where(d => d.ParentAlias == ParentAlias);
            rptList.DataBind();
        }

        protected void rptList_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                rptList.Visible = true;
                //var childRepeater = (e.Item.FindControl("rptChildren") as usercontrols_NestedRepeater);
                var childPlaceholder = (e.Item.FindControl("plcChildren") as PlaceHolder);


                ContentComparison contentComparison = ((e.Item.DataItem) as ContentComparison);
                string parent = contentComparison.ParentAlias;

                usercontrols_NestedRepeater childRepeater =
                    (usercontrols_NestedRepeater) LoadControl("NestedRepeater.ascx");
                childPlaceholder.Controls.Add(childRepeater);

                childRepeater.Data = Data;
                childRepeater.ParentAlias = contentComparison.Alias;
                childRepeater.DataBind();
            }
        }
    }
}